// pages/user/shop_info/shop_info.js
var tcity = require("../../../citys.js");

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      provinces: [],
      province: "",
      citys: [],
      city: "",
      countys: [],
      county: '',
      value: [0, 0, 0],
      values: [0, 0, 0],
      condition: false
  },

  bindChange: function (e) {
      var val = e.detail.value
      var t = this.data.values;
      var cityData = this.data.cityData;

      if (val[0] != t[0]) {
          console.log('province no ');
          const citys = [];
          const countys = [];

          for (let i = 0; i < cityData[val[0]].sub.length; i++) {
              citys.push(cityData[val[0]].sub[i].name)
          }
          for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
              countys.push(cityData[val[0]].sub[0].sub[i].name)
          }

          this.setData({
              province: this.data.provinces[val[0]],
              city: cityData[val[0]].sub[0].name,
              citys: citys,
              county: cityData[val[0]].sub[0].sub[0].name,
              countys: countys,
              values: val,
              value: [val[0], 0, 0]
          })

          return;
      }
      if (val[1] != t[1]) {
          console.log('city no');
          const countys = [];

          for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
              countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
          }

          this.setData({
              city: this.data.citys[val[1]],
              county: cityData[val[0]].sub[val[1]].sub[0].name,
              countys: countys,
              values: val,
              value: [val[0], val[1], 0]
          })
          return;
      }
      if (val[2] != t[2]) {
          console.log('county no');
          this.setData({
              county: this.data.countys[val[2]],
              values: val
          })
          return;
      }


  },
  open: function () {
      this.setData({
          condition: !this.data.condition
      })
  },
  onLoad: function () {
      console.log("onLoad");
      var that = this;

      tcity.init(that);

      var cityData = that.data.cityData;


      const provinces = [];
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
      }
      console.log('省份完成');
      for (let i = 0; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
      }
      console.log('city完成');
      for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
          countys.push(cityData[0].sub[0].sub[i].name)
      }

      that.setData({
          'provinces': provinces,
          'citys': citys,
          'countys': countys,
          'province': cityData[0].name,
          'city': cityData[0].sub[0].name,
          'county': cityData[0].sub[0].sub[0].name
      })
      console.log('初始化完成');
  },

  shop_info:function(e){
      var that = this;
      var shop_name = e.detail.value.shop_name;
      var shop_owner = e.detail.value.shop_owner;
      var shop_province = e.detail.value.shop_province;
      var shop_city = e.detail.value.shop_city;
      var shop_county = e.detail.value.shop_county;
      var shop_address = e.detail.value.shop_address;
      console.log(shop_province + shop_city + shop_county)
      app.ajax({
          url: app.host + 'mall/Login/shop_info',
          data:({
              shop_name: shop_name,
              shop_owner: shop_owner,
              shop_province: shop_province,
              shop_city: shop_city,
              shop_district: shop_county,
              shop_address: shop_address,
          }),
          success:function(res){
              console.log(res);
              var dt = res.data;
              if (dt.code==1){
                    app.data.is_login = true;
                    wx.showToast({
                        title: dt.msg,
                        icon: 'success',
                        duration: 2000,
                        success: function (msg) {
                            wx.navigateBack({
                                delta: 2
                            })
                        },
                    })
                }else{
                    console.log('--------')
                    console.log(dt.msg)
                    app.showTopTips(dt.msg, that);
                }
          },
      })
  },
})